using System;
using System.Collections.Generic;
using System.Web;

internal class TagHandlerFactory : IHttpHandlerFactory
{
    private readonly Queue<TagHandler> pool = new Queue<TagHandler>();
    
    /// <summary>
    /// Hack!!! A set of tags that lasts across any number of web requests,
    /// while this instance of the web app is alive.
    /// </summary>
    private static readonly Dictionary<String, Tag> tags = new Dictionary<String, Tag>(); 

    public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
    {
        lock (pool)
        {
            if (pool.Count != 0)
                return pool.Dequeue();
        }
        return new TagHandler(tags);
    }

    public void ReleaseHandler(IHttpHandler handler)
    {
        lock (pool)
        {
            pool.Enqueue((TagHandler) handler);
        }
    }
}